package ${pPackage}.common.utils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class DownloadUtils {
  public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException {
    response.setContentType("application/octet-stream");
    returnName = response.encodeURL(new String(returnName.getBytes(), "iso8859-1"));
    response.addHeader("content-disposition", "attachment;filename=" + returnName);
    response.setContentLength(byteArrayOutputStream.size());
    ServletOutputStream outputstream = response.getOutputStream();
    byteArrayOutputStream.writeTo(outputstream);
    byteArrayOutputStream.close();
    outputstream.flush();
  }
}
